package com.qk.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qk.command.impl.AcceptCommand;
import com.qk.command.impl.AnswerCommand;
import com.qk.command.impl.AskCommand;
import com.qk.command.impl.ListCommand;
import com.qk.command.impl.ScoreCommand;
import com.qk.my.pojos.Player;

public class PlayerFactory extends SystemFactory {
	private Player player;
	private static Map<CommandCode, Class<? extends Command>> map = new HashMap<CommandCode, Class<? extends Command>>();
	static {
		map.put(CommandCode.LIST, ListCommand.class);
		map.put(CommandCode.ASK, AskCommand.class);
		map.put(CommandCode.ANSWER, AnswerCommand.class);
		map.put(CommandCode.ACCEPT, AcceptCommand.class);
		map.put(CommandCode.SCORE, ScoreCommand.class);
	}

	public PlayerFactory(Player player) {
		// TODO Auto-generated constructor stub
		this.player = player;
	}

	@Override
	public Command buildCommand(CommandCode code) {
		// TODO Auto-generated method stub
		Class<? extends Command> command = map.get(code);
		if (command != null) {
			try {
				Constructor<? extends Command> con = command.getConstructor(new Class[] { Player.class });
				return con.newInstance(new Object[] { player });
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return super.buildCommand(code);
	}

}
